﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CapaDeEntidades;
using System.Data.SqlClient;
using System.Data;

namespace CapaDatos
{
   public class PrestamoDatos
    {
       public static string connectionstring = "Initial Catalog=Biblioteca;Data Source=localhost;Integrated Security=SSPI;";


       public static bool RegistrarDevolucionPrestamo(Prestamo prestamo)
       {
           bool respu = false;
           SqlConnection conn = new SqlConnection(connectionstring);
           
           try
           {
               
               int idPrestamo = 0;

               if (prestamo.IdPrestamo != null)
               {
                   idPrestamo = prestamo.IdPrestamo;
               }


               SqlCommand cmd = new SqlCommand("sp_ActualizarPrestamo", conn);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@idPrestamo", idPrestamo);
               cmd.Parameters.AddWithValue("@alta_devolucion", respu);
               cmd.Parameters["@alta_devolucion"].Direction = ParameterDirection.Output;
               cmd.ExecuteNonQuery();
               respu = Convert.ToBoolean(cmd.Parameters["@alta_devolucion"].Value);
           }
           catch (Exception ex)
           {
               System.Diagnostics.Debug.WriteLine(ex.Message);
               System.Diagnostics.Debug.WriteLine(ex.StackTrace);
           }
           finally
           {
               if (conn != null)
                   conn.Close();
           }
           return respu;
       }

       public static bool AltaPrestamo(Prestamo prestamo)
       {
           bool respu = false;
           SqlConnection conn = new SqlConnection(connectionstring);

           try
           {
               SqlCommand cmd = new SqlCommand("sp_CrearPrestamo", conn);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@fechaPedido ", prestamo.FechaPrestamo);
               cmd.Parameters.AddWithValue("@idLibro", prestamo.Libro.Codigo);
               cmd.Parameters.AddWithValue("@idSocio", prestamo.Socio.Codigo);
               cmd.Parameters.AddWithValue("@fechaDevolucion", prestamo.FechaDevolucion);
               cmd.Parameters.AddWithValue("@alta_prestamo", respu);
               respu = Convert.ToBoolean(cmd.ExecuteScalar());

           }
           catch (Exception ex)
           {
               System.Diagnostics.Debug.WriteLine(ex.Message);
               System.Diagnostics.Debug.WriteLine(ex.StackTrace);
           }
           finally
           {
               if (conn != null)
                   conn.Close();
           }
           return respu;
       }




    }
}
